package org.dromara.visitor.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.visitor.domain.constants.ApplyStatusEnum;
import org.dromara.visitor.domain.entity.Apply;
import org.dromara.visitor.mapper.ApplyMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component
public class ApplyStatusExpireJob {
    @Resource
    private ApplyMapper applyMapper;

    @Scheduled(cron = "0 */5 * * * ?")
    public void executeExpire(){
        log.info("开始执行过期定时任务");
        LocalDateTime now = LocalDateTime.now();
        LambdaQueryWrapper<Apply> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Apply::getStatus, ApplyStatusEnum.APPLY_FIRST.getCode(), ApplyStatusEnum.APPLY_SECOND.getCode());
        wrapper.le(Apply::getExpireTime, now);
        List<Apply> applyList = applyMapper.selectList(wrapper);
        for (Apply apply : applyList) {
            apply.setStatus(ApplyStatusEnum.EXPIRE.getCode());
            apply.setUpdateTime(now);
        }
        applyMapper.updateById(applyList);
        log.info("结束执行过期定时任务");
    }
}
